import sys
import os

# locate app-engine SDK
AE_PATH = "/cygdrive/c/Program Files (x86)/Google/google_appengine/"
#AE_PATH = "/usr/local/google_appengine/"

# path to app code
APP_PATH = os.path.abspath(".")

# load the AE paths (as stolen from dev_appserver.py)
EXTRA_PATHS = [
    APP_PATH,
    AE_PATH,
    os.path.join(AE_PATH, 'lib', 'antlr3'),
    os.path.join(AE_PATH, 'lib', 'django'),
    os.path.join(AE_PATH, 'lib', 'ipaddr'),
    os.path.join(AE_PATH, 'lib', 'webob'),
    os.path.join(AE_PATH, 'lib', 'yaml', 'lib'),
    os.path.join(AE_PATH, 'lib', 'fancy_urllib'), # issue[1]
]
sys.path = EXTRA_PATHS + sys.path

import unittest
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import testbed

from datetime import *
from time import *
import random 

from Person import *
from Activity import *
from Location import *
from plans import Home
from plans import Work
from util import *
from Calendar import *
from Planner import *

class T(unittest.TestCase):

    def setUp(self):
        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Next, declare which service stubs you want to use.
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

    def tearDown(self):
        self.testbed.deactivate()

    def testCreatePerson(self):
        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.save()
      
        p2 = LoadPerson("thomas.the.red@gmail.com")
        self.assertEqual("Clausen", p2.lastName)

    def testCreateLocation(self):
        l = Location()
        l.number = 56
        l.save()

        l2 = LoadLocation(1)
        self.assertEqual(56, l2.number)

    def testCreatePersonWithLocation(self):
        l = Location()
        l.number = 56
        l.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l
        p.save()

        p2 = LoadPerson(p.email)
        self.assertEqual(56, p2.homeLocation.number)

    def testSetPersonCoordinates(self):
        l = Location()
        l.number = 7
        l.zipCode = 2200
        l.street = "Frejasgade"
        l.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l
        p.save()
        
        SetPersonLocationCoordinates(p)

        self.assertEqual( 55.698420900000002, p.homeLocation.lat)
        self.assertEqual( 12.5517897, p.homeLocation.lng)

    def testPersonTravel(self):
        l = Location()
        l.number = 7
        l.zipCode = 2200
        l.street = "Frejasgade"
        l.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l
        p.save()
        
        SetPersonLocationCoordinates(p)

        self.assertEqual( 55.698420900000002, p.homeLocation.lat)
        self.assertEqual( 12.5517897, p.homeLocation.lng)

    def INACTIVEtestMap(self):
        mapcode = GetMapCode(Location(lat=100.0,lng=100.0), None)

        self.assertEqual("\n<script src=\"http://maps.google.co", mapcode[:35])

    def testActivity(self):
        a1 = Activity(name="Working hard...")
        a1.save()
        a2 = Activity(name="Not working at all...")
        a2.save()

        activities = LoadAllActivities(10)
        for a in activities:
            print a.id, a.name


    def testCalendar(self):
        # Argument is user id
        cal = Calendar(1)

        a1 = Activity(name="Working hard...")
        a1.put()
        d = localDatetime(2011, 11, 02, 13, 01)
        cal.addEntry(a1, d)

        a2 = Activity(name="Not working at all...")
        a2.put()
        d = localDatetime(2011, 11, 02, 17, 01)
        cal.addEntry(a2, d)

        d = localDatetime(2011, 11, 02, 14, 01)
        a1g = cal.getActivityAtTime(d)
        self.assertEqual(a1.name, a1g.name)

        d = localDatetime(2011, 11, 02, 18, 01)
        a2g = cal.getActivityAtTime(d)
        self.assertEqual(a2.name, a2g.name)

        d1 = localDatetime(2010, 11, 02, 12, 01)
        d2 = localDatetime(2011, 11, 02, 16, 01)

        entries = cal.getEntriesInInterval(d1, d2)
        self.assertEqual(1, len(entries))

        #for e in entries:
        #    print e.date, e.activity.name

    def testNormalDist(self):
        d1 = localDatetime(2010, 11, 02, 12, 01, 0)
        d2 = getGaussDistDate(d1, 3600)
        #print d2

    def testPersonCalendar(self):
        l = Location()
        l.number = 7
        l.zipCode = 2200
        l.street = "Frejasgade"
        l.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l
        p.save()

        cal = p.getCalendar()

        a1 = Activity(name="Working hard...")
        a1.put()
        cal.addEntry(a1, TimeNow(), 2.0, None)
        d = localDatetime(2012, 4, 15, 17, 1)
        cal.addEntry(a1, d, 2.0, None)

        for e in cal.getEntriesToday():
            print e.date, e.activity.name, e.duration

        print cal.getCurrentActivity().name

    def testPersonCheckIn(self):
        l = Location()
        l.number = 7
        l.zipCode = 2200
        l.street = "Frejasgade"
        l.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l
        p.save()

        a1 = Activity(name="Working hard...")
        a1.put()

        p.addCheckIn(a1, TimeNow(), l)

        for e in p.getCheckInsToday():
            print e.date, e.activity.name, e.location


    def gtestPlanner(self):
        person = Person()
        ph = Home.Home()
        pw = Work.Work()
        d = localDatetime(2011, 12, 05, 10, 0, 0)
        pha = ph.activity(person, d)
        pwa = pw.activity(person, d)
        print pha, pwa
        if pha[0] > 0:
            d = pha[1]
        elif pwa[0] > 0:
            d = pwa[1]
        print d
        print ph.activity(person, d)[0], pw.activity(person, d)[0]

    def testDist(self):
        l1 = Location()
        l1.number = 7
        l1.zipCode = 2200
        l1.street = "Frejasgade"
        SetLocationCoordinates(l1)

        l2 = Location()
        l2.number = 3
        l2.zipCode = 2200
        l2.street = "Frejasgade"
        SetLocationCoordinates(l2)

        print Dist(l1, l2)

    def testTimeNow(self):
        print TimeNowUTC()
        print TimeNow()
        

    def testPersonSetLocation(self):
        l1 = Location()
        l1.number = 7
        l1.zipCode = 2200
        l1.street = "Frejasgade"
        l1.save()

        l2 = Location()
        l2.number = 5
        l2.zipCode = 2670
        l2.street = "Agern Alle"
        l2.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l1
        p.workLocation = l2
        p.currentLocation = None
        p.save()

        date = localDatetime(2011, 12, 05, 17, 10)

        SetPersonLocationCoordinates(p)

        p.setLocation(date)

        print p.currentLocation.lat, p.currentLocation.lng
        print Distance(p.currentLocation, p.homeLocation)

        trip = Trip_getOrCreate(p.id, l2, l1)
        trip.userDuration = 0.2
        trip.save()

        p.setLocation(date)

        print p.currentLocation.lat, p.currentLocation.lng
        print Distance(p.currentLocation, p.homeLocation)

    def testDirections(self):
        d = Directions(1, 2)
        print TravelTime(d)
        p = PositionAlongDirections(d, timedelta(minutes=0))
        print p
        p = PositionAlongDirections(d, timedelta(minutes=1))
        print p
        p = PositionAlongDirections(d, timedelta(minutes=2))
        print p
        p = PositionAlongDirections(d, timedelta(minutes=3))
        print p

    def testTrip(self):
        l1 = Location()
        l1.number = 7
        l1.zipCode = 2200
        l1.street = "Frejasgade"
        l1.save()

        l2 = Location()
        l2.number = 5
        l2.zipCode = 2670
        l2.street = "Agern Alle"
        l2.save()

        p = Person()
        p.lastName = "Clausen"
        p.email = "thomas.the.red@gmail.com"
        p.homeLocation = l1
        p.save()

        trip = getOrCreateTrip(34, l1, l2)
        print trip.duration
        

if __name__ == '__main__':
    unittest.main()

